package com.ibeeking.found.file.rest.service.util;

import cn.hutool.core.util.IdUtil;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.file.service.common.dos.UserFileDO;
import com.ibeeking.nematos.utils.date.DateUtil;

import java.time.LocalDateTime;

public class QiwenFileUtil {


    public static UserFileDO getQiwenDir(Long userId, String filePath, String fileName) {
        UserFileDO userFile = new UserFileDO();
        userFile.setUserFileId(IdUtil.getSnowflakeNextId());
        userFile.setUserId(userId);
        userFile.setFileId(null);
        userFile.setFileName(fileName);
        userFile.setFilePath(QiwenFile.formatPath(filePath));
        userFile.setExtendName(null);
        userFile.setIsDir(1);
        userFile.setUploadTime(LocalDateTime.now());
        userFile.setCreateBy(UserUtils.getUserId());
        userFile.setCreateTime(LocalDateTime.now());
        userFile.setDeleteFlag(0);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFileDO getQiwenFile(Long userId, Long fileId, String filePath, String fileName, String extendName) {
        UserFileDO userFile = new UserFileDO();
        userFile.setUserFileId(IdUtil.getSnowflakeNextId());
        userFile.setUserId(userId);
        userFile.setFileId(fileId);
        userFile.setFileName(fileName);
        userFile.setFilePath(QiwenFile.formatPath(filePath));
        userFile.setExtendName(extendName);
        userFile.setIsDir(0);
        userFile.setUploadTime(LocalDateTime.now());
        userFile.setCreateTime(LocalDateTime.now());
        userFile.setCreateBy(UserUtils.getUserId());
        userFile.setDeleteFlag(0);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFileDO searchQiwenFileParam(UserFileDO userFile) {
        UserFileDO param = new UserFileDO();
        param.setFilePath(QiwenFile.formatPath(userFile.getFilePath()));
        param.setFileName(userFile.getFileName());
        param.setExtendName(userFile.getExtendName());
        param.setDeleteFlag(0);
        param.setUserId(userFile.getUserId());
        param.setIsDir(0);
        return param;
    }

    public static String formatLikePath(String filePath) {
        String newFilePath = filePath.replace("'", "\\'");
        newFilePath = newFilePath.replace("%", "\\%");
        newFilePath = newFilePath.replace("_", "\\_");
        return newFilePath;
    }

}
